package com.test.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;


public class SplashActivity extends Activity {

    private ImageView imgBj;
    public static final String URL ="http://cdn.iciba.com/web/news/longweibo/imag/2015-06-09.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        initialize();

        final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Intent  intent  = new Intent();
                intent.setClass(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);

        ImageRequest irequest = new ImageRequest(URL, new Response.Listener<Bitmap>() {
                    public void onResponse(Bitmap bitmap) {
                        imgBj.setBackgroundDrawable(new BitmapDrawable(SplashActivity.this.getResources(), bitmap));
                        new Thread(new Runnable(){
                            public void run(){
                                try {
                                    Thread.sleep(2000);
                                    System.gc();
                                    handler.sendMessage(new Message());
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }

                        }).start();
                    }
                }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
            }
        });
        requestQueue.add(irequest);


    }



    private void initialize() {

        imgBj = (ImageView) findViewById(R.id.imgBj);
    }
}
